import { Component, OnInit, ViewChild } from '@angular/core';
import { STComponent, STColumn, STColumnTag, STData } from '@delon/abc';
import { _HttpClient, ModalHelper, SettingsService } from '@delon/theme';
import { NzNotificationService } from 'ng-zorro-antd';
import { SysParamEditComponent } from './edit/sysparam-edit.component';

const BADGE_DICTTYPE: STColumnTag = {
  '0': { text: '业务类', color: 'blue' },
  '1': { text: '系统类', color: 'red' },
};
const BADGE_PARAM_TYPE: STColumnTag = {
  '0': { text: '默认', color: 'green' },
  '1': { text: '报表', color: 'orange' },
  '2': { text: '安全', color: 'red' },
  '3': { text: '消息', color: 'blue' },
  '9': { text: '其它', color: 'purple' },
};

@Component({
  selector: 'app-system-sysparam',
  templateUrl: './sysparam.component.html',
})
export class SysParamComponent implements OnInit {
  auth_btn_add = false;
  auth_btn_edit = false;
  auth_btn_del = false;
  url = `/cf/admin/param/page`;
  params = { key: '' };

  @ViewChild('st', { static: false }) st: STComponent;
  columns: STColumn[] = [
    { title: '名称', index: 'name' },
    { title: '键', index: 'key' },
    { title: '值', index: 'value' },
    { title: '类型', index: 'publicType', type: 'tag', tag: BADGE_PARAM_TYPE },
    {
      title: '参数分类',
      index: 'system',
      type: 'tag',
      tag: BADGE_DICTTYPE,
      filter: {
        menus: [{ text: '系统类', value: '1' }, { text: '业务类', value: '0' }],
        multiple: false,
      },
    },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          type: 'static',
          modal: { component: SysParamEditComponent, paramsName: 'record' },
          click: 'reload',
          iif: (item: STColumn) => {
            return this.auth_btn_edit && item.system !== '1';
          },
        },
        {
          text: '删除',
          type: 'del',
          pop: {
            title: '确定删除参数？',
            okType: 'danger',
          },
          click: (record, _modal, comp) => {
            this.http.delete(`/cf/admin/param/${record.id}`).subscribe(res => {
              if (res.status === 200) {
                this.notice.success('成功', `成功删除【${record.key}】`);
                comp!.load();
              }
            });
          },
          iif: (item: STColumn) => {
            return this.auth_btn_del && item.system !== '1';
          },
        },
      ],
    },
  ];

  constructor(
    private http: _HttpClient,
    private modal: ModalHelper,
    private notice: NzNotificationService,
    private settings: SettingsService,
  ) {}

  ngOnInit() {
    this.auth_btn_add = this.settings.user.permissions.admin_syspublicparam_add;
    this.auth_btn_edit = this.settings.user.permissions.admin_syspublicparam_edit;
    this.auth_btn_del = this.settings.user.permissions.admin_syspublicparam_del;
  }

  add() {
    this.modal.createStatic(SysParamEditComponent, { i: { id: 0 } }).subscribe(() => this.st.reload());
  }
}
